package com.milo.coding.concurrent.lock;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Auther milo
 * @Description
 * @Date 2021-04-03 17:02
 */
public class ReadWriteLockDemo {
    private static final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    private static ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
    private static ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();

    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(()->read()).start();
        new Thread(()->read()).start();
        new Thread(()->write()).start();
        new Thread(()->write()).start();
    }

    private static void read() {
        String threadName = Thread.currentThread().getName();
        // readLock.lock();
        lock.lock();
        try {
            System.out.println(threadName + "-获取读锁(readLock)，读取数据...");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println(threadName + "-释放读锁(readLock)");
            // readLock.unlock();
            lock.unlock();
        }
    }

    private static void write() {
        String threadName = Thread.currentThread().getName();
        // writeLock.lock();
        lock.lock();
        try {
            System.out.println(threadName + "-获取写锁(writeLock)，写入数据...");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println(threadName + "-释放写锁(writeLock)");
            // writeLock.unlock();
            lock.unlock();
        }
    }
}
